//
//  CLsmallViewController.m
//  I-want-go
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLsmallViewController.h"
#import "CLarticleCollectionCell.h"
#import "CLtool.h"
#import "BaseModel.h"
#import "CLunder.h"
#import "CLinterViewController.h"
#import "CLscreen.h"
@interface CLsmallViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *myCollectionView;

@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, assign)NSInteger page;

@end

@implementation CLsmallViewController

- (void)dealloc {
    [_arr release];
    [_myCollectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor purpleColor];
    
//    NSLog(@"%@", self.mytitle);
//    self.title = self.mytitle;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(170 * SWIDTH, 200 * SHEIGHT);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 602 * SHEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    
    [_myCollectionView release];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView registerClass:[CLarticleCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    
    self.myCollectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    
    self.myCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];

    
    [self createData];
}

- (void)header {
    self.page = 1;
    [self.arr removeAllObjects];
    [self createData];
}

- (void)footerAction {
    
    self.page = 0;
    self.page++;
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr =[NSString stringWithFormat: @"fromType=-1&imageHD=2&key=fde439a5bd66f0327b52bdf0d3d696d5&outCategoryId=%@&page=%ld&t=1445930291393&userid=705811&version=3.5.4", self.sssId, (long)self.page];
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arArr = [NSMutableArray array];
        arArr = [CLunder baseModelByArr:dic[@"data"][@"records"]];
        for (CLunder *under in arArr) {
            [self.arr addObject:under];
        }
        [self.arr removeLastObject];
        [self.myCollectionView.footer endRefreshing];
        [self.myCollectionView reloadData];
        
    }];
}

- (void)createData {
    
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *sqq = [NSString stringWithFormat:@"fromType=-1&imageHD=2&key=fde439a5bd66f0327b52bdf0d3d696d5&outCategoryId=%@&page=0&t=1445930291393&userid=705811&version=3.5.4", self.sssId];
    NSString *bodyStr = sqq;
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        self.arr = [NSMutableArray array];
        self.arr = [CLunder baseModelByArr:dic[@"data"][@"records"]];
        [self.myCollectionView reloadData];
    }];
    [self.myCollectionView.header endRefreshing];
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count - 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CLarticleCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    CLunder *und = self.arr[indexPath.row];
    
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:und.image]];
    
    cell.addLabel.text = und.brand;
    cell.proLabel.text = und.productDescription;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CLinterViewController *intVC = [[CLinterViewController alloc] init];
    [self.navigationController pushViewController:intVC animated:YES];
    
    CLunder *under = self.arr[indexPath.row];
    
    intVC.interId = [NSString stringWithFormat:@"%@", under.productId];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
